package com.card.framework.mybatis.mapper;

import java.sql.PreparedStatement;
import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import com.card.framework.mybatis.MyFrameworkSqlSession;

@Service("MyFrameworkSupperMapper")
public class MyFrameworkSupperMapper {

	@Resource
	private MyFrameworkSqlSession sqlSession;
	
	
    public <T> T getMapper(Class<T> type) {
        return sqlSession.getMapper(type);
    }
	 /**
     * {@inheritDoc}
     */
    public Object selectOne(String statement) {
        return this.sqlSession.selectOne(statement);
    }

    /**
     * {@inheritDoc}
     */
    public Object selectOne(String statement, Object parameter) {
        return this.sqlSession.selectOne(statement, parameter);
    }

    /**
     * {@inheritDoc}
     */
    public List<?> selectList(String statement) {
        return this.sqlSession.selectList(statement);
    }

    /**
     * {@inheritDoc}
     */
    public List<?> selectList(String statement, Object parameter) {
        return this.sqlSession.selectList(statement, parameter);
    }   
    
    /**
     * {@inheritDoc}
     */
    public int insert(String statement) {
        return this.sqlSession.insert(statement);
    }

    /**
     * {@inheritDoc}
     */
    public int insert(String statement, Object parameter) {
        return this.sqlSession.insert(statement, parameter);
    }

    /**
     * {@inheritDoc}
     */
    public int update(String statement) {
        return this.sqlSession.update(statement);
    }

    /**
     * {@inheritDoc}
     */
    public int update(String statement, Object parameter) {
        return this.sqlSession.update(statement, parameter);
    }

    /**
     * {@inheritDoc}
     */
    public int delete(String statement) {
        return this.sqlSession.delete(statement);
    }

    /**
     * {@inheritDoc}
     */
    public int delete(String statement, Object parameter) {
        return this.sqlSession.delete(statement, parameter);
    }
    
    
    
    public<T>void batchSubmit(Class<T> type,BatchCallBack batchCallBack) throws Exception
    {
    	SqlSession session = this.sqlSession.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
    	try {
    		batchCallBack.batchCallBack(session.getMapper(type));
    		session.commit();
    		session.clearCache();
    	} catch (Exception e) {
    		//没有提交的数据可以回滚
    		session.rollback();
    		throw e;
    	} finally {
    		session.close();
    	}
    }
    
    public<T>void executeSQL(String sql,ExecuteSQLCallBack callBack) throws Exception
    {
    	SqlSession session = this.sqlSession.getSqlSessionFactory().openSession();
    	PreparedStatement pstmt = session.getConnection().prepareStatement(sql);
    	try {
    		callBack.executeSQLCallBack(pstmt);
    		session.commit();
    		session.clearCache();
    	} catch (Exception e) {
    		//没有提交的数据可以回滚
    		session.rollback();
    		throw e;
    	} finally {
    		session.close();
    	}
    }
}
